import { Injectable } from '@nestjs/common';
import { Delete, DeleteResults, Result, Select, SelectOne, SelectOneResults, SelectResults } from 'decorator-mysql';
import { User } from './entity/user.entity';
import { Article } from './entity/article.entity';

@Injectable()
export class AppMapping {

    // 正常查询
    @Select('select * from user')
    selectAllUser: () => SelectResults<any>

    // 查询一条数据,并且传递基础类型参数
    @SelectOne('select * from user where user_id = &{0} limit 1')
    selectUserById: (userId: number) => SelectOneResults<any>

    // 查询一条数据,并且传递对象类型参数
    @SelectOne('select * from user where user_id = &{userId} limit 1')
    selectUserById2: (user: User) => SelectOneResults<any>


    // 查询数据,并将数据进行字段转换
    @Result(User)
    @Select('select * from user')
    selectResultUser: () => SelectResults<User>

    // 查询数据,并只要特定字段
    @Result(User, { only: ['userName'] })
    @Select('select * from user')
    selectResultUser2: () => SelectResults<User>

    // 查询数据,并排除特定字段
    @Result(User, { exclude: ['password'] })
    @Select('select * from user')
    selectResultUser3: () => SelectResults<User>

    // 查询数据,并排除添加自定义的SQL
    @Result(User, { custom: '1 + 1 as count' })
    @Select('select * from user')
    selectResultUser4: () => SelectResults<User>

    // 多表查询
    @Result(Article, { alias: 'a' })
    @Result(User, { alias: 'u' })
    @Select('select * from user u left join article a on u.user_id = a.user_id;')
    selectResultUser5: () => SelectResults<User>

    // 删除一条数据
    @Delete('delete from user where user_id = 1')
    deleteUserById: () => DeleteResults
}
